var b=a
let a_1
let ye = 1
yeshu(a)
xianshi(1)
//页面编码
function yeshu(a) {
    maxyema = Math.ceil(a.length / 4)
    for (let i = 1; i <= maxyema; i++) {
        $('.yema').append(`<div class="aa${i}" onclick="xianshi(${i})">${i}</div>`)
    }
}

//渲染内容
function xianshi(n) {
    ye = n
    num = (n - 1) * 4
    a_1 = a.slice(num, num + 4)
    $('.neirong').html('')
    for (
        let i = 0; i < a_1.length; i++
    ) {
        $('.neirong').append(`
            <tr align="center">
                <td class="td1"><input type="checkbox" style="width: 15px;"><img src="${a_1[i].tp}"></td>
                <td>${a_1[i].bt}</td>
                <td>${a_1[i].kc}</td>
                <td>${a_1[i].xsj}</td>
                <td>${a_1[i].fl}</td>
                <td>${a_1[i].pp}</td>
                <td>${a_1[i].gj}</td>
                <td>${a_1[i].xl}</td>
                <td>${a_1[i].sj}</td>
                <td class="cz" > <button onclick="xiajia(${i})">下架</button><button onclick="sanchu(${i})">删除</button><button>评论管理</button></td>
                </tr>
            `)
    }
    $('.yema>div').eq(ye - 1).css('backgroundColor', '#108ee9');
    $('.yema>div').eq(ye - 1).siblings().css('backgroundColor', '');
}

// 上一页
function shangyiye() {
    if (
        ye > 1
    ) {
        ye = ye - 1

        xianshi(ye)

    }
}
// 下一页
function xiayiye() {
    maxyema = Math.ceil(a.length / 4)
    if (
        ye < maxyema
    ) {
        ye = ye + 1

        xianshi(ye)

    }
}
//全选
$('.qx').change(
    function () {
        $('td>input').prop('checked', $(this).prop('checked'))
    }
)
$('td>input').change(function () {
    if (
        $('td>input:checked').length == a_1.length) {
        $('.qx').prop('checked', true)
    } else {
        $('.qx').prop('checked', false)
    }
})
// 下架
let xiabiao
let n1
function xiajia(n) {
    $('.xj1').css('display', 'block')
    xiabiao = (ye - 1) * 4 + n
    n1 = n
    console.log(xiabiao)
}
$('.qr').click(function () {

    if (a[xiabiao].sj == "是") {
        a[xiabiao].sj = "否"
        xianshi(ye)
        $('.xj>div').eq(0).text("你确定上架该商品吗？")
        $('.cz').eq(n1).children().eq(1).text("上架")
    } else {
        a[xiabiao].sj = "是"
        xianshi(ye)
        $('.xj>div').eq(0).text("你确定下架该商品吗？")
        $('.cz').eq(n1).children().eq(1).text("下架")
    }
    $('.xj1').css('display', 'none')
})
$('.qx').click(function () {
    $('.xj1').css('display', 'none')
})
// 删除
function sanchu(n) {
    $('.sq1').css('display', 'block')
    xiabiao = (ye - 1) * 4 + n
}
$('.qx1').click(function () {
    $('.sq1').css('display', 'none')
})
$('.qr1').click(function () {
    a.splice(xiabiao, 1)
    $('.yema').html("")
    yeshu(a)
    xianshi(ye)
    $('.sq1').css('display', 'none')
})
//设置预警值
$('.yjz').click(function () {
    $('.kc1').css('display', 'block')
})
$('.qx2').click(function () {
    $('.kc1').css('display', 'none')
})
$('.qr2').click(function(){
    let val = $('.yjz2').val()
    $('.yjz1').text($('.yjz2').val())
    $('.kc1').css('display', 'none')
})
//排序方式
function compare(property){
    return function(a,b){
        let value1 = a[property];
        let value2 = b[property];
        return value2 - value1;
    }
}
$('.pai>select').change(function(){
let px=$('.pai>select option:selected').val()
if( px==1){
    a=a.sort(compare('xl'))
    xianshi(1)
}
else if(px==2){
    a=a.sort(compare('xsj'))
    xianshi(1)
}
})
let aa=a.length
$('.top>span').text("商品列表(共"+aa+"条)")



